package com.village.zpark.controller;

import com.github.pagehelper.PageInfo;
import com.village.zpark.common.ResultMapUtil;
import com.village.zpark.pojo.VillageInfo;
import com.village.zpark.service.VillageInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/villageInfo")
public class VillageInfoController {
    @Autowired
    private VillageInfoService villageInfoService;



    @RequestMapping
    public String villageInfo(){
        return "/villageInfo";
    }

    @RequestMapping(value = "/villageInfoPage")
    public String villageInfoPage(){
        return "/villageInfoPage";
    }

    @RequestMapping(value = "/villageInfoQueryById")
    public Object villageInfoQueryById(@RequestParam(name = "id",required = true) Integer id, Model model){
        VillageInfo villageInfo = villageInfoService.findById(id);
        model.addAttribute("obj",villageInfo);
        return "/villageInfoPage";
    }

    @RequestMapping(value = "/villageInfoList")
    @ResponseBody
    public Object villageInfoList(){
        List<VillageInfo> villageInfoList = villageInfoService.findAll();
        return ResultMapUtil.getHashMapList(villageInfoList);
    }


    @RequestMapping(value = "/addvillageInfo")
    @ResponseBody
    public Object addvillageInfo(VillageInfo villageInfo){
        try {
            int i = villageInfoService.addVillageInfo(villageInfo);
            return ResultMapUtil.getHashMapSave(i);
        } catch (Exception e) {
            return ResultMapUtil.getHashMapException(e);
        }
    }


    @RequestMapping(value = "/updatevillageInfo")
    @ResponseBody
    public Object updatevillageInfo(VillageInfo villageInfo){
        try {
            int i = villageInfoService.updateVillageInfo(villageInfo);
            return ResultMapUtil.getHashMapSave(i);
        } catch (Exception e) {
            return ResultMapUtil.getHashMapException(e);
        }
    }

    @RequestMapping(value = "/deletevillageInfo")
    @ResponseBody
    public Object deletevillageInfo(Integer id){
        try {
            int i = villageInfoService.deleteVillageInfoById(id);
            return ResultMapUtil.getHashMapDel(i);
        } catch (Exception e) {
            return ResultMapUtil.getHashMapException(e);
        }
    }


    @RequestMapping(value = "/villageInfoQueryPage")
    @ResponseBody
    public Map<String, Object> villageInfoQueryPage(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer limit, String param) {
        PageInfo<VillageInfo> villageInfoPageInfo = villageInfoService.findPage(page, limit, param);
        Map<String,Object> map=new HashMap<>();
        map.put("code", 0);
        map.put("msg", "查询数据成功");
        map.put("count", villageInfoPageInfo.getTotal());
        map.put("data", villageInfoPageInfo.getList());
        return map;
    }

}
